﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;

namespace CRM.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DebugController : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly IServiceCollection _services;

        public DebugController(MyDbContext dbContext, IServiceCollection services)
        {
            _dbContext = dbContext;
            _services = services;
        }
        [HttpGet("[action]")]
        public async void Services()
        {
            var context = HttpContext;
            context.Response.ContentType = "text/html; charset=utf-8";
            await context.Response.WriteAsync($"<h1>所有服务{_services.Count}个</h1><table><thead><tr><th>类型</th><th>生命周期</th><th>Instance</th></tr></thead><tbody>");
            foreach (var svc in _services)
            {
                await context.Response.WriteAsync("<tr>");
                await context.Response.WriteAsync($"<td>{svc.ServiceType.FullName}</td>");
                await context.Response.WriteAsync($"<td>{svc.Lifetime}</td>");
                await context.Response.WriteAsync($"<td>{svc.ImplementationType?.FullName}</td>");
                await context.Response.WriteAsync("</tr>");
            }
            await context.Response.WriteAsync("</tbody></table>");
        }
    }
}